package Chat;
// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com 

import java.io.*;
import java.sql.ResultSet;
import java.text.Normalizer.Form;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Timer;

import javax.swing.text.AbstractDocument.BranchElement;

import Entity.ActivityReport;
import Entity.EstimationReport;
import Entity.Extention_Stage_req;
import Entity.FailureReport;
import Entity.Extention_Stage_req;
import Entity.MsgPackage;
import Entity.Request;
import Entity.TardinessReport_ENTITY;
import Entity.User;




import ocsf.server.*;

/**
 * This class overrides some of the methods in the abstract superclass in order
 * to give more functionality to the server.
 * 
 * @author Dr Timothy C. Lethbridge
 * @author Dr Robert Lagani&egrave;re
 * @author Fran&ccedil;ois B&eacute;langer
 * @author Paul Holden
 * @version July 2000
 */
public class EchoServer extends AbstractServer {

	//private Timer timer;
	//private ScheduledTask onceADayTask;

//	private Timer timer;
	//private ScheduledTask onceADayTask;

	private mysqlConnection sqlc;
	private Object result;

	//Constructors ****************************************************

	/**
	* Constructs an instance of the server.
	*
	* @param port The port number to connect on.
	*/
	public EchoServer(int port) 
	{
		super(port);
		
		sqlc=new mysqlConnection();
		result=null;

		//onceADayTask  = new ScheduledTask();
	//	timer = new Timer();

	//	onceADayTask  = new ScheduledTask();
		//timer = new Timer();

		/** Setting the task to run once a day starting in 5 minutes */
		//timer.scheduleAtFixedRate(onceADayTask, 5*60*1000, 24*60*60*1000);

	}


	//Instance methods ************************************************

	/**
	* This method handles any messages received from the client.
	* It redirects it to the database class and receives an answer as
	* an object and sends it back to client
	*
	* @param msg The object message received from the client.
	* @param client The connection from which the message originated.
	*/
	public void handleMessageFromClient(Object msg, ConnectionToClient client) {
		MsgPackage new_msg= (MsgPackage)msg;
		
		switch (new_msg.getOp_code()){
	    case  LOGIN:
	    	User user=(User) new_msg.getObject();
	    	result=sqlc.loggin_check(user);
	    	break;
	    case GET_PERMISSIONS:
	    	User user1=(User) new_msg.getObject();
	    	result=sqlc.get_prmissions(user1);
	    	break;
	    case ADD_REQUEST:
	    	Request req = (Request) new_msg.getObject();
	    	result = sqlc.addRequest(req);
	    	break;
	    case DETERMINE_MANAGER:
	    	int [] stageManagers = (int[]) new_msg.getObject();
	    	result= sqlc.insertStageManagerQuery(stageManagers);
	    	break;
	    case GET_ENGINEERS:
	    	result= sqlc.get_info_eng();
	        break;
	    case GET_ACTIVE:
	    	
	    	result=sqlc.getActiveRequest();
	    	break;
	    case DETERMINE_DURATION:
	    	int [] stageDuration = (int[]) new_msg.getObject();
	    	result= sqlc.insertStageDurationQuery(stageDuration);
	    	break;
	    case CURREN_DURATION:
	    	Integer requestID = (Integer) new_msg.getObject();
	    	result= sqlc.getCurrentDur(requestID);
	    	break;
	    case OUT_PUT_REQ:
	    	Integer reqID = (Integer) new_msg.getObject();
	    	result= sqlc.getReqDetails(reqID);
	    	break;
	    case GET_EX_REQ:
	    	result = sqlc.getWaitingReqQuery();
	    	break;
	    case SET_ESTIMATION_STAGE:
	    	EstimationReport estRep = (EstimationReport) new_msg.getObject();
	    	result= sqlc.setEstimationReportQuery(estRep);
	    	break;
	    case GET_ESTIMATOR:
	    	Integer r_id = (Integer) new_msg.getObject();
	    	result= sqlc.getSystemInchargQuery(r_id);
	    	break;
	    case GET_CHAIRMAN:
	    	result= sqlc.getChairManQuery();
	    	break;
	    case GET_COMMITTEE:
	    	result= sqlc.getCommittee();
	    	break;
	    case OUT_PUT_ESTIMATION_REPORT:
	    	Integer i=(Integer) new_msg.getObject();
	    	result= sqlc.getEstimationReportQuery(i);
	    	break;
	    case SET_EXAMINATION_STAGE:
	    	String [] data= (String[]) new_msg.getObject();
	    	result= sqlc.setExaminationStage(data);
	    	break;
	    case WAITING_LIST:
	    	result= sqlc.getWatingReq();
	    	break;
	    case LOG_OUT:
	    	Integer userId = (Integer) new_msg.getObject();
	    	result= sqlc.logOut(userId);
	    	break;
	    case SET_PERFORMANCE_STAGE:
	    	Integer reqId = (Integer) new_msg.getObject();
	    	result= sqlc.setPerformanceDecision(reqId);
	    	break;
	    case SET_FAILURE_REPORT:
	    	FailureReport fr = (FailureReport) new_msg.getObject();
	    	result= sqlc.setFailureReport(fr);
	    	break;
	    case SET_TESTING_STAGE:
	    	Integer req_id = (Integer) new_msg.getObject();
	    	result= sqlc.setTstingStage(req_id);
	    	break;
	    case EXTENTION_STAGE:
	    	Extention_Stage_req exStageReq = (Extention_Stage_req) new_msg.getObject();
	    	result= sqlc.CheckExtentionStageQuery(exStageReq);
	    	break;
	   case EXTEND_REQ:
	    	Extention_Stage_req exten=(Extention_Stage_req) new_msg.getObject();
	    	result= sqlc.extend_stage(exten);
	    	break;
	    case REJECT_REQ:
	    	Extention_Stage_req exten1=(Extention_Stage_req) new_msg.getObject();
	    	result= sqlc.update_exte_req_rejected(exten1);
	    	break;
	    case VIEW_REQ:
	    	Integer n=(Integer) new_msg.getObject();
	    	result= sqlc.viewReq(n.intValue());
	    	break;
	    case UPDATE_AUTHORIZATION_COMM:
	    	User u= (User) new_msg.getObject();
	    	result =sqlc.updateCommitteeMember(u);
	    	break;
	    case UPDATE_AUTHORIZATION_SUP_OR_CHAIR:
	    	User x= (User) new_msg.getObject();
	    	result =sqlc.updateSupervisorORchairmanQuery(x);
	    	break;
	    case UPDATE_AUTHORIZATION:
	    	User y= (User) new_msg.getObject();
	    	result =sqlc.updateInfoEngneer(y);
	    	break;
	    case FREEZE_REQ:
	    	Integer req1=(Integer) new_msg.getObject();
	    	result=sqlc.freez_req(req1);
	    	break;
	    case GET_CLOSE_REQ:
	    	result=sqlc.get_req_to_close();
	    	break;
	    case CLOSE_REQ:
	    	Integer req2=(Integer) new_msg.getObject();
	    	result=sqlc.close_req(req2.intValue());
	    	break;
	    case GET_INITIATOR:
	    	Integer request=(Integer) new_msg.getObject();
	    	result=sqlc.get_initiator(request.intValue());
	    	break;
	    case SET_DURATION_STAGE_MAMAGER:
	    	String [] data1 = (String[]) new_msg.getObject();
	    	result=sqlc.setStageManagerDuration(data1);
	    	break;
	    case ACTIVITY_REPORT:
	    	ActivityReport a =(ActivityReport) new_msg.getObject();
	    	result=sqlc.getActivityReportQuery(a);
	    	break;
	    case TARDINESS_REP:
	    	TardinessReport_ENTITY b = (TardinessReport_ENTITY) new_msg.getObject();
	    	result = sqlc.tardiness_report(b);
	    	break;
	    case ACTIVITY_GRAPH:
	    	String[] a1 = (String[]) new_msg.getObject();
	    	result=sqlc.Acti(a1);
	    case UNFREEZE_REQ:
	    	Integer req_unf = (Integer) new_msg.getObject();
	    	result=sqlc.unfreezReq(req_unf);
	    	break;
	    case GET_UNFREEZE_REQ:
	    	result=sqlc.get_frozen_requests();
	    	break;
	    default:
			break;
	    	
		
		
		}
		try {
			client.sendToClient(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		}
	

	/**
	 * This method overrides the one in the superclass. Called when the server
	 * starts listening for connections.
	 */
	protected void serverStarted() {
		System.out.println("Server listening for connections on port "
				+ getPort());
	}

	/**
	 * This method overrides the one in the superclass. Called when the server
	 * stops listening for connections.
	 */
	protected void serverStopped() {
		System.out.println("Server has stopped listening for connections.");
	}

	// Class methods ***************************************************

	

// End of EchoServer class
}
	
